<?php
namespace app\admin\controller;
use think\Db;
class CityController extends CommonController{
    /**
     * @return string
     * 列表首页
     */
    public function index(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table('ew_city')->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_city')->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * @return string
     * 删除
     */
    public function deleteCity(){
        $cid = $_POST['cid'];
        if ($cid != '') {
            $where['city_id'] = $cid;
            $res = Db::table('ew_city')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }

    /***
     * 修改页面
     */
    public function updateCity(){
        $cid = $_POST['nid'];
        $res = Db::table('ew_city')->where('city_id',$cid)->find();
        $this->assign('res',$res);
        return $this->fetch('city/update');
    }

    /**
     * 修改功能
     */
    public function update(){
        if(!empty($_FILES['fileImg']['type'])){
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileImg']['name']);
            $filePath = $_FILES['fileImg']['tmp_name'];
            $img_name = $rand.'.'.$exe[1];
            $_filePath = 'static/img/city/'.$img_name;
            move_uploaded_file($filePath,$_filePath);
            $data['city_thumb'] = $_filePath;
        }
        if(trim($_POST['city_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "城市不能为空";
            return json_encode($returnData);
        }
        $where1['city_name'] = $_POST['city_name'];
        $where1['city_id'] = array('neq',$_POST['city_id']);
        $result = Db::table('ew_city')->where($where1)->find();
        if($result['city_id']!=''){
            $returnData['status'] = 0;
            $returnData['msg'] = "城市名称不能重复";
            return json_encode($returnData);
        }
        $data['city_name'] = $_POST['city_name'];
        $res = Db::table('ew_city')->where('city_id',$_POST['city_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }

    /*
     * 添加页面
     */
    public function add(){
        return $this->fetch('city/add');
    }

    public function insertCity(){
        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['city_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "城市不能为空";
            return json_encode($returnData);
        }
        $result = Db::table('ew_city')->where('city_name','=',$_POST['city_name'])->find();
        if($result['city_id']!=''){
            $returnData['status'] = 0;
            $returnData['msg'] = "城市名称不能重复";
            return json_encode($returnData);
        }
        //1
        $rand = date('YmdHis').rand(100000,999999);
        $exe = explode('.',$_FILES['fileImg']['name']);
        $filePath = $_FILES['fileImg']['tmp_name'];
        $img_name = $rand.'.'.$exe[1];
        $_filePath = 'static/img/city/'.$img_name;
        move_uploaded_file($filePath,$_filePath);
        $data['city_thumb'] = $_filePath;
        $data['city_name'] = $_POST['city_name'];
        $res = Db::table('ew_city')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }

    public function behavior(){
         $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where['city_id'] = $_POST['behavior_id'];
        $res = Db::table('ew_behavior')->where($where)->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_behavior')->where($where)->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * @return string
     * 删除
     */
    public function deleteBehavior(){
        $cid = $_POST['cid'];
        if ($cid != '') {
            $where['behavior_id'] = $cid;
            $res = Db::table('ew_behavior')->where($where)->delete();
            if ($res) {
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            } else {
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }
    }

    /**
     * @return mixed
     * 行为添加
     */
    public function addB(){
        $city_id = $_POST['behavior_id'];
        $this->assign('city_id',$city_id);
        return $this->fetch('city/addBehavior');
    }

    public function addInsert(){
        if($_FILES['fileImg']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if($_FILES['fileImg2']['name']==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "图片不能为空";
            return json_encode($returnData);
        }
        if(trim($_POST['city_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "行为名称不能为空";
            return json_encode($returnData);
        }
        //1
        $rand = date('YmdHis').rand(100000,999999);
        $exe = explode('.',$_FILES['fileImg']['name']);
        $filePath = $_FILES['fileImg']['tmp_name'];
        $img_name = $rand.'.'.$exe[1];
        $_filePath = 'static/img/city/'.$img_name;
        move_uploaded_file($filePath,$_filePath);
        //2
        $rand2 = date('YmdHis').rand(100000,999999);
        $exe2 = explode('.',$_FILES['fileImg2']['name']);
        $filePath2 = $_FILES['fileImg2']['tmp_name'];
        $img_name2 = $rand2.'.'.$exe2[1];
        $_filePath2 = 'static/img/city/'.$img_name2;
        move_uploaded_file($filePath2,$_filePath2);

        $data['behavior_img'] = $_filePath;
        $data['behavior_pic'] = $_filePath2;
        $data['behavior_name'] = $_POST['city_name'];
        $data['city_id'] = $_POST['city_id'];
        $res = Db::table('ew_behavior')->insert($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "添加成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "添加失败！"));
        }
    }

    /**
     * 行为修改
     */
    public function updateB(){
        $cid = $_POST['nid'];
        $res = Db::table('ew_behavior')->where('behavior_id',$cid)->find();
        $this->assign('res',$res);
        return $this->fetch('city/updateBehavior');
    }


    public function updateBehavior(){
        if(!empty($_FILES['fileImg']['type'])){
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['fileImg']['name']);
            $filePath = $_FILES['fileImg']['tmp_name'];
            $img_name = $rand.'.'.$exe[1];
            $_filePath = 'static/img/city/'.$img_name;
            move_uploaded_file($filePath,$_filePath);
            $data['behavior_img'] = $_filePath;
        }
        if(!empty($_FILES['fileImg2']['type'])){
            $rand2 = date('YmdHis').rand(100000,999999);
            $exe2 = explode('.',$_FILES['fileImg2']['name']);
            $filePath2 = $_FILES['fileImg2']['tmp_name'];
            $img_name2 = $rand2.'.'.$exe2[1];
            $_filePath2 = 'static/img/city/'.$img_name2;
            move_uploaded_file($filePath2,$_filePath2);
            $data['behavior_pic'] = $_filePath2;
        }
        if(trim($_POST['behavior_name'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "行为名称不能为空";
            return json_encode($returnData);
        }

        $data['behavior_name'] = $_POST['behavior_name'];
        $res = Db::table('ew_behavior')->where('behavior_id',$_POST['behavior_id'])->update($data);
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "修改成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "修改失败";
            return json_encode($returnData);
        }
    }

}